package org.example.serviceEdu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.example.ResultCode.R;
import org.example.serviceEdu.entity.EduTeacher;
import org.example.serviceEdu.entity.vo.TeacherQuery;
import org.example.serviceEdu.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-10-14
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/serviceEdu/teacher")
@Api(description="讲师管理")
public class EduTeacherController {


        @Autowired
        private EduTeacherService eduTeacherService;


    //查询讲师表所有数据
        @GetMapping("/findAll")
        @ApiOperation(value = "所有讲师列表")
        public R list(){
            List<EduTeacher> list=eduTeacherService.list(null);

            return R.ok().data("items",list);
        }
//        删除方法

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/deleteTeacherById/{id}")
        public R deleteTeacherById(@ApiParam (name ="id",value="讲师ID",required = true)@PathVariable String id){
            boolean flag=eduTeacherService.removeById(id);
            if(flag){
                return R.ok();
            }else{
                return R.error();

            }

        }
        @ApiOperation(value = "分页老师列表")
        @GetMapping("/pageList/{page}/{limit}")
        public  R pageList(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
            Page<EduTeacher> pageParam=new Page<>(page,limit);
            eduTeacherService.page(pageParam,null);
            List<EduTeacher > records=pageParam.getRecords();//获取当前页的记录列表
            Long total=pageParam.getTotal();
            return R.ok().data("total",total).data("rows",records);
        }
        @ApiModelProperty(value = "新增讲师")
        @PostMapping("/save")
        public R save(@RequestBody  EduTeacher eduTeacher){

            boolean flag=eduTeacherService.save(eduTeacher);
            if(flag){
                return R.ok();
            }else {
                return R.error();
            }

        }
        @ApiModelProperty(value = "根据Id查询")
        @GetMapping("/getById/{id}")
        public  R getById(@PathVariable String id){
            EduTeacher teacher=eduTeacherService.getById(id);
            return  R.ok().data("teacher",teacher);
        }
        @ApiModelProperty(value="修改讲师")
        @PostMapping("/updateById")
        public  R updateById(@RequestBody EduTeacher teacher){
            boolean flag=eduTeacherService.updateById(teacher);
            if(flag){
                return R.ok();
            }else{
                return  R.error();
            }

        }
    //多条件查询讲师带分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher>pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询，mybatis===>动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);//模糊查询
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }




}




